//
//  FriendListCellTableViewCell.m
//  QSBK
//
//  Created by Eric Wang on 5/6/15.
//  Copyright (c) 2015 Eric Wang. All rights reserved.
//
#import "UIImageView+WebCache.h"
#import "FriendListCell.h"

@implementation FriendListCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        [self addAllSubviews];
    }
    return self;
}

-(void)addAllSubviews
{
//    @property(nonatomic,strong)UIImageView *headImage;
    self.headImage = [[UIImageView alloc]init];
    [self.headImage.layer setCornerRadius:20];
    self.headImage.layer.masksToBounds = YES;
    self.headImage.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.headImage];
//    @property(nonatomic,strong)UILabel *nameLabel;
    self.nameLabel = [[UILabel alloc]init];
    self.nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.nameLabel];
//    @property(nonatomic,strong)UILabel *messageLabel;
    self.messageLabel = [[UILabel alloc]init];
    self.messageLabel.font = [UIFont systemFontOfSize:14];
    self.messageLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.messageLabel];
//    @property(nonatomic,strong)UILabel *timeLabel;
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.font = [UIFont systemFontOfSize:14];
    self.timeLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.timeLabel];
//    @property(nonatomic,strong)UIButton *noticeButton;
    self.noticeButton = [[UIButton alloc]init];
    self.noticeButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.noticeButton];
    
    _constraint = [[NSMutableArray alloc]init];
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(20)-[headImage(40)]-(>=0)-|" options:0 metrics:nil views:@{@"headImage":self.headImage}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(10)-[headImage(40)]-(10)-|" options:0 metrics:nil views:@{@"headImage":self.headImage}]];
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[headImage]-(10)-[nameLabel(150)]-(>=0)-|" options:0 metrics:nil views:@{@"headImage":self.headImage,@"nameLabel":self.nameLabel}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(10)-[nameLabel(20)]-(>=0)-|" options:0 metrics:nil views:@{@"nameLabel":self.nameLabel}]];
    
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[headImage]-(10)-[messageLabel(200)]-(>=0)-|" options:0 metrics:nil views:@{@"headImage":self.headImage,@"messageLabel":self.messageLabel}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[nameLabel]-(>=0)-[messageLabel(20)]-(0)-|" options:0 metrics:nil views:@{@"nameLabel":self.nameLabel,@"messageLabel":self.messageLabel}]];
    
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[timeLabel(100)]-(10)-|" options:0 metrics:nil views:@{@"timeLabel":self.timeLabel}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(10)-[timeLabel(10)]-(>=0)-|" options:0 metrics:nil views:@{@"timeLabel":self.timeLabel}]];
    
    
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[noticeButton(20)]-(0)-|" options:0 metrics:nil views:@{@"noticeButton":self.noticeButton}]];
    [_constraint addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[timeLabel]-(5)-[noticeButton(20)]-(5)-|" options:0 metrics:nil views:@{@"timeLabel":self.timeLabel,@"noticeButton":self.noticeButton}]];
    
    [self.contentView addConstraints:_constraint];
}


-(void)setUser:(User *)user
{
    _user = user;
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:_user.icon] placeholderImage:[UIImage imageNamed:@"default_users_avatar.png"]];
    self.nameLabel.text = _user.login;
    self.messageLabel.text = user.role;
    NSDate *date = [[NSDate alloc]init];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *str = [formatter stringFromDate:date];
    self.timeLabel.text = str;
}
@end
